import java.io.File;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import com.almworks.sqlite4java.SQLiteConnection;
import com.almworks.sqlite4java.SQLiteException;
import com.almworks.sqlite4java.SQLiteStatement;


public class DBParser {
	
	private final static String jp1 = "redpin_JP1_24_12_2012.db";
	private final static String dcc1 = "redpin_daycare_centre_24_12_2012.db";
	private final static String dcc2 = "redpin_daycare_centre_3_01_2013.db";

	/**
	 * @param args
	 * @throws SQLiteException 
	 */
	public static void main(String[] args) throws SQLiteException 
	{
		ArrayList<VisitedLocation> visitedLocations = new ArrayList<VisitedLocation>();
		
		VisitedLocation previousLoc = null;
		
		SQLiteConnection db = new SQLiteConnection(new File("C://Users//colin//Documents//Magic Briefcase//MSc Documents//Research Thesis//Pilot Study//"+dcc2));
	    db.open(true);
	    
	    SQLiteStatement st = db.prepare("SELECT loc.symbolicId, v1.timestamp" +
	    		" FROM visitedlocation v1, location loc" +
	    		" WHERE v1.locationId = loc.locationId");
	    try {
    	
	      while (st.step()) 
	      {
	    	if(previousLoc == null)
	    	{
	    		previousLoc = new VisitedLocation(null, st.columnString(0), epochToDate(st.columnString(1)));
	    		visitedLocations.add(previousLoc);
	    	}
	    	else
	    	{
	    		VisitedLocation currentLoc= new VisitedLocation(previousLoc, st.columnString(0), epochToDate(st.columnString(1)));
	    		visitedLocations.add(currentLoc);
	    		previousLoc = currentLoc;
	    	}    
	        
	      }
	    } finally {
	      st.dispose();
	    }
	    
	    printArray(visitedLocations);
	    System.out.println("-------- ------- --------");
	    getOutcome(visitedLocations);
	    
	    //printUniquePaths(getAmountOfVisits(visitedLocations));
	    
	    db.dispose();
	}

	private static void printArray(ArrayList<VisitedLocation> visitedLocations) 
	{
		System.out.println("PreviousLoc  CurrentLoc  DateTime");
		for(VisitedLocation e : visitedLocations)
		{
			if(e.getPreviousLocation() != null)
			{
				System.out.println(e.getPreviousLocation().getLocationName() + " " + e.getLocationName() + " " + e.getDateTime().toLocaleString());
			}
		}
	}
	
	

	private static Date epochToDate(String timestamp) 
	{
		Date date = new Date(Long.parseLong(timestamp));
		return date;
	}
	
	private static void getOutcome(ArrayList<VisitedLocation> visitedLocations)
	{
		ArrayList<VisitedLocation> locChanges = new ArrayList<VisitedLocation>();
		
		for(VisitedLocation e : visitedLocations)
		{
			if(e.getPreviousLocation() != null)
			{
				if(e.getPreviousLocation().getLocationName().compareTo(e.getLocationName()) != 0)
				{
					locChanges.add(e);
					//System.out.println(e.getPreviousLocation().getLocationName() + " " + e.getLocationName() + " " + e.getDateTime().toLocaleString());
				}
			}
		}
	}
	
	private static ArrayList<VisitedLocationUnique> getAmountOfVisits(ArrayList<VisitedLocation> locChanges)
	{
		ArrayList<VisitedLocationUnique> locChangesUnique = new ArrayList<VisitedLocationUnique>();
		
		for(VisitedLocation e : locChanges)
		{
			if(pathExists(e, locChangesUnique))
			{
				addCounter(e, locChangesUnique);
			}
			else
			{
				addUniqueRecord(e, locChangesUnique);
			}
		}
		
		return locChangesUnique;
	}

	private static void addCounter(VisitedLocation e, ArrayList<VisitedLocationUnique> locChangesUnique) 
	{
		for(VisitedLocationUnique locU : locChangesUnique)
		{
			if(locU.getPreviousLoc().compareTo(e.getPreviousLocation().getLocationName()) == 0 && locU.getCurrentLoc().compareTo(e.getLocationName()) == 0)
			{
				locU.addCounter();
				break;
			}
		}
		
	}
	
	private static void addUniqueRecord(VisitedLocation e, ArrayList<VisitedLocationUnique> locChangesUnique) 
	{
		String previousLoc = e.getPreviousLocation().getLocationName();
		String currentLoc = e.getLocationName();
		
		VisitedLocationUnique vlu = new VisitedLocationUnique(previousLoc, currentLoc, 1);
	}

	private static boolean pathExists(VisitedLocation loc, ArrayList<VisitedLocationUnique> locChangesUnique) 
	{
		for(VisitedLocationUnique e : locChangesUnique)
		{
			if(loc.getPreviousLocation().getLocationName().compareTo(e.getPreviousLoc()) == 0 && loc.getLocationName().compareTo(e.getCurrentLoc()) == 0)
			{
				return true;
			}
		}
		return false;
	}
	
	private static void printUniquePaths(ArrayList<VisitedLocationUnique> outcome) 
	{
			for(VisitedLocationUnique e : outcome)
			{
				System.out.println(e.getPreviousLoc() + ">" + e.getCurrentLoc() + "=" + e.getCounter() + "times");
			}
	}
	
}
